#!/bin/sh

if [ $# != 2 ]; then
    echo "Usage: $0 <master_ip> <path_to_bind_chroot>"
    exit 1
fi

MASTERIP=$1
BINDCHROOT=$2

#1.init sync&cron enviroment 
#===========================

#1.1 prepare restrictive shell - smrsh
#-------------------------------------
if [ -x /usr/sbin/smrsh ];then
    # CentOS
    SMRSH_CMD=/usr/sbin/smrsh
    SMRSH_ALLOWCMD_DIR=/etc/smrsh
elif [ -x /usr/lib/sendmail.d/bin/smrsh ];then
    # OpenSuSE
    SMRSH_CMD=/usr/lib/sendmail.d/bin/smrsh
    SMRSH_ALLOWCMD_DIR=/usr/lib/sendmail.d/bin
elif [ -x /usr/libexec/smrsh ];then
    # FreeBSD
    SMRSH_CMD=/usr/libexec/smrsh
    SMRSH_ALLOWCMD_DIR=/usr/libexec/sm.bin
else
    echo "Error: can't find smrsh, exiting!"
    exit 1
fi

if ! which rsync > /dev/null 2>&1 ; then
    echo "Error: can't find rsync, exiting!"
    exit 1
fi

if ! grep smrsh /etc/shells >/dev/null 2>&1 ; then
   echo "$SMRSH_ALLOWCMD_DIR/smrsh">>/etc/shells
fi

rm -f $SMRSH_ALLOWCMD_DIR/rsync
ln -s `which rsync` $SMRSH_ALLOWCMD_DIR/

#1.2 make user(xbaydns) for sync
#-----------------------------------
rm -rf /home/xbaydns
userdel xbaydns
useradd xbaydns -g named -s $SMRSH_CMD -d /home/xbaydns

rm -rf  /home/xbaydns
mkdir -p /home/xbaydns
mkdir -p /home/xbaydns/{.ssh,prog,view,slave,agent}
mkdir -p /home/xbaydns/view/{iplatency,idcview}
mkdir -p /home/xbaydns/slave/{prog,named/{log,etc/{acl,view,dynamic}}}
mkdir -p /home/xbaydns/agent/{prog,iplatency}

touch /home/xbaydns/.ssh/authorized_keys

cp -r `xdwherepkg`/xbaydns/tools/prober/* /home/xbaydns/agent/prog
chmod +x /home/xbaydns/agent/prog/*
echo "MASTER_IP=\"$MASTERIP\"">/home/xbaydns/agent/agent.conf
echo "MASTER_IP=\"$MASTERIP\"">/home/xbaydns/master.conf


cp -r `xdwherepkg`/xbaydns/tools/slave/* /home/xbaydns/slave/prog
chmod +x /home/xbaydns/slave/prog/*
echo "MASTER_IP=\"$MASTERIP\"">/home/xbaydns/slave/slave.conf

cp -r `xdwherepkg`/xbaydns/tools/master/* /home/xbaydns/prog
chmod +x /home/xbaydns/prog/*

crontab -u xbaydns -l >/home/xbaydns/old_crontab 2>/dev/null
crontab -u xbaydns /home/xbaydns/prog/crontab

#2.prepare bind's chroot
#-----------------------
mkdir -p $BINDCHROOT/{etc,dev,var/{run,dump,stats,log}}
chown -R named:named $BINDCHROOT/var/{run,dump,stats}
touch $BINDCHROOT/var/log/named.log
chown -R named:named $BINDCHROOT/var/log/named.log

rm -rf $BINDCHROOT/dev/{random,zero,null}
mknod $BINDCHROOT/dev/random c 1 8
mknod $BINDCHROOT/dev/zero c 1 5
mknod $BINDCHROOT/dev/null c 1 3

#3.set envionment variables
#==========================
echo "\
XBAYDNS_CHROOT_PATH=$BINDCHROOT
XBAYDNS_BIND_CONF=etc
XBAYDNS_BIND_START=/usr/local/sbin/named
XBAYDNS_BIND_USER=named
export PATH XBAYDNS_CHROOT_PATH XBAYDNS_BIND_CONF XBAYDNS_BIND_START XBAYDNS_BIND_USER
"|tee /home/xbaydns/xdenv

chmod +x /home/xbaydns/xdenv
source /home/xbaydns/xdenv
id named || useradd named -s /sbin/nologin
xdinitbind master
chown -R named:named $BINDCHROOT
chown -R xbaydns:named /home/xbaydns
chmod -R 700 /home/xbaydns
rm -f /etc/rndc.key
ln -s $BINDCHROOT/etc/rndc.key /etc/
echo "\



XBayDNS(enabled master) installed successfully!
MASTER    :$MASTERIP
HOME      :/home/xbaydns

The next thing you may do:
1. Start named server and make sure named running normally
   #/usr/local/sbin/named -t $XBAYDNS_CHROOT_PATH -u named
   #tail -f /var/log/messages
2. Start XBayDNS Master Web server
   #xdweb
3. Browse http://$MASTERIP username/password for administrator: admin/admin
   DON'T FORGET TO CHANGE YOUR PASSWORD!
"
